/**
 * 
 */
package br.com.livraria.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author Jeferson
 *
 */
public class AbstractDao {

	String url = "jdbc:mysql://localhost:3306";//127.0.0.1
	String db = "/livraria";
	String usuario = "root";
	String senha = "250585";
	
	Connection connection = null;
	
	private Connection getConnection()  throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		if(connection == null) {
			try {
				connection = DriverManager.getConnection(url+db, usuario, senha);
				return connection;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			return connection;
		}
		return null;
	}
	
	public Connection open() {
		if(this.connection == null) {
			try {
				return getConnection();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	public void close() {
		if(this.connection != null) {
			try {
				this.connection.close();
				this.connection = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
